package com.hp.auth_service.controller;

import com.hp.auth_service.entity.Functions;
import com.hp.auth_service.service.IFunctionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FunctionController {

    @Autowired
    private IFunctionsService functionsService;

    @GetMapping("/functions/username/{username}")
    public ResponseEntity<List<Functions>> getFunctionsByUsername(@PathVariable String username){
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        if (principal instanceof User && principal != null) {
//            User user = (User) principal;
//            //按用户名查询菜单权限
//            List<Functions> functions = functionsService.getFunctionsByUsername(user.getUsername());
//            return ResponseEntity.ok(functions);
//        }
//        return ResponseEntity.ok(null);
        //按用户名查询菜单权限
        List<Functions> functions = functionsService.getFunctionsByUsername(username);
        return ResponseEntity.ok(functions);
    }

    @RequestMapping("/functions")
    public ResponseEntity<List<Functions>> getAllFunctions(){
        return ResponseEntity.ok(functionsService.getAllFunctions());
    }

    @RequestMapping("/functions/role/{roleId}")
    public ResponseEntity<List<String>> getFunctionsIdsByRoleId(@PathVariable String roleId){
        return ResponseEntity.ok(functionsService.getFunctionsIdsByRoleId(roleId));
    }
}
